package com.dk.util;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

/**
 * http相关的日志
 *
 * @author: A11181121050450
 * @date: 2020-05-09 09:53
 */
@Slf4j
public class HttpLogUtil {
    public static void printHeader(HttpServletRequest request){
        StringBuffer sb = new StringBuffer();
        Enumeration<String> enumeration = request.getHeaderNames();
        enumeration.hasMoreElements();
        for(;;){
            String key = enumeration.nextElement();
            sb.append(key).append(":");
            Enumeration<String> vs = request.getHeaders(key);
            if (! vs.hasMoreElements())
                sb.append("[]");
            else {
                sb.append('[');
                for (; ; ) {
                    sb.append(vs.nextElement());
                    if (!vs.hasMoreElements()) {
                        sb.append(']').toString();
                        break;
                    }
                    sb.append(',').append(' ');
                }
            }
            if(!enumeration.hasMoreElements()){
                break;
            }
            sb.append("\n");
        }
        log.info("url:"+request.getRequestURI()+"\n" +
                "header:"+sb.toString()+"\n");
    }
}
